﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MOJsdk;
using System.Collections.ObjectModel;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_PluginsSettings.xaml
    /// </summary>
    public partial class Control_PluginsSettings : UserControl
    {
        private MojSystem _system;
        private ObservableCollection<MojPlugin> _plugins;

        public Control_PluginsSettings()
        {
            InitializeComponent();

            _system = MojHost.GetHost.MoJSystem;
            INIStyle();
            INIEvents();
        }

        private void INIStyle()
        {
            _btnActivation.IsEnabled = false;

            _btnRemove.IsEnabled = false;
            _btnRemove.Content = MOJLocalization.Localize("Remove");

            _btnInstall.Content = MOJLocalization.Localize("Install");

            _grid.ApplyStyle("");
            _listView.ApplyStyle("");
            _btnActivation.ApplyStyle("");
            _btnInstall.ApplyStyle("");
            _btnRemove.ApplyStyle("");
        }
        private void INIEvents()
        {
            _btnActivation.Click += ActivateClick;
            _btnInstall.Click += InstallClick;
            _btnRemove.Click += RemoveClick;
            _listView.SelectionChanged += SelectionChanged;
            Loaded += delegate { LoadPlugins(); };
        }

        #region Events
        public void ActivateClick(object sender, RoutedEventArgs e)
        { 
            int x = _listView.SelectedIndex;
            if(x==-1) return;
            _system.Plugins.ChangeActiveStatus(_plugins[x]);
            _system.Plugins.CheckPlugins();
            LoadPlugins();
        }
        public void InstallClick(object sender, RoutedEventArgs e)
        { 

        }
        public void RemoveClick(object sender, RoutedEventArgs e)
        {
            int x = _listView.SelectedIndex;
            if (x == -1) return;

            _plugins[x].Plugin.Unistall();
            FileManageTools.AddToDelete(_plugins[x].PluginPath);

            if (_plugins[x].IsActivated)
                _system.Plugins.ChangeActiveStatus(_plugins[x]);

            MojHost.GetHost.ShowMessage("Plugin Removed!", "Warning");

            _system.Plugins.CheckPlugins();
            LoadPlugins();
        }
        public void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int x = _listView.SelectedIndex;

            _btnActivation.IsEnabled = x != -1;
            _btnRemove.IsEnabled = x != -1;

            if(x!=-1)
                _btnActivation.Content = _plugins[x].IsActivated ? 
                    MOJLocalization.Localize("Deactivate") : MOJLocalization.Localize("Activate");

        }
        #endregion

        private void LoadPlugins()
        {
            _listView.ItemsSource = null;
            _system.Plugins.CheckPlugins();
            _plugins = new ObservableCollection<MojPlugin>(_system.Plugins.Plugins);
            _listView.ItemsSource = _plugins;
        }
    }
}
